package com.fauks.study.msweb.config;

import com.fauks.study.msweb.component.MyLocalResolver;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebMvcConfig {
    @Bean
    public WebMvcConfigurer webMvcConfigurationSupport() {
        return new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
                //添加首页映射
                registry.addViewController("/main.html").setViewName("dashboard");
            }

            //为了不影响其他测试测试注释
            /*@Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new LoginHandlerInterceptor())
                        .addPathPatterns("/emp")//拦截所有请求
                        //不拦截的请求,对于静态资源SpringBoot已经映射好无需配置
                        .excludePathPatterns("/emp/login", "/", "/index.html");
            }*/
        };
    }

    //错误页面处理
   /* @Bean
    public ConfigurableServletWebServerFactory configurableServletWebServerFactory() {
        TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
        tomcatServletWebServerFactory.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/error-400.html"));
        tomcatServletWebServerFactory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error-404.html"));
        tomcatServletWebServerFactory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error-500.html"));
        return tomcatServletWebServerFactory;
    }*/

    //国际化处理器
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocalResolver();
    }
}
